package com.eec.AOP.responsibility;

import com.eec.AOP.Chain;
import com.eec.AOP.proceedingJoinPoint.ProceedingJoinPointImpl;
import com.eec.spring.ioc.IOCContainer;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author LeeC
 * @description Around通知责任类
 * @date 2025/8/15
 */
public class ResponsibilityAround extends BaseResponsibility implements Responsibility {

    public ResponsibilityAround(Class<?> CLASS, Method method, Class<?> aspectClass) {
        super.annotationClass = CLASS;
        super.method = method;
        super.aspectClass = aspectClass;
    }

    @Override
    public Object invoke(Chain chain) {
        Object singleBean = IOCContainer.getSingleBean(aspectClass);

        try {
            // 创建 ProceedingJoinPoint 对象
            ProceedingJoinPointImpl joinPoint = new ProceedingJoinPointImpl(chain);
            // 调用 around 通知方法，将控制权交给通知方法
            Object result = method.invoke(singleBean, joinPoint);

            return result;
        } catch (IllegalAccessException | InvocationTargetException e) {
            throw new RuntimeException("Error invoking around advice", e);
        }
    }

    @Override
    public Class<?> getAnnotationClass() {
        return super.annotationClass;
    }

    @Override
    public void setAnnotationClass(Class<?> annotationClass) {
        super.annotationClass = annotationClass;
    }
}
